package org.argeproje.resim.proc.view;

import javax.media.jai.JAI;
import javax.swing.JPanel;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Panel;
import java.awt.image.BufferedImage;
import java.awt.image.renderable.ParameterBlock;
import java.io.IOException;

import org.argeproje.resim.ResimProjectEditor;
import org.argeproje.resim.ViewerPage;
import org.argeproje.resim.proc.ConnectionRule;
import org.argeproje.resim.proc.data.Data;
import org.argeproje.resim.proc.data.ExtremaDA;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.DefaultCategoryDataset;

public class ExtremaViewerPR extends ViewerPR{
	protected void setConnectionRules(ConnectionRule connRule){
		connRule.addInType(ExtremaDA.class, 0);
	}
	@Override
	public Data process() {
		 ExtremaDA data = getInputAsExtremaDA();
	     String minStr = "Minimum";
	     String maxStr = "Maximum";
	     int nBands = data.getNBands();
	     String[] bandStrs = new String[nBands];
	     for(int i = 0; i < nBands; i++){
	    	 bandStrs[i] = (i+1) + ". Band";
	     }
	     DefaultCategoryDataset dataset = new DefaultCategoryDataset();
	     double[] maxs = data.getMaximas();
	     double[] mins = data.getMinimas();
	     for(int i = 0; i < nBands; i++){
	    	dataset.addValue(maxs[i], maxStr, bandStrs[i]);
	    	dataset.addValue(mins[i], minStr, bandStrs[i]);
	     }
	       JFreeChart chart = ChartFactory.createBarChart(
				"Image Pixel Maximas and Minimas", // chart title
				"Bands", // domain axis label
				"Value", // range axis label
				dataset, // org.argeproje.resim.proc.data
				PlotOrientation.VERTICAL, // orientation
				true, // include legend
				true, // tooltips?
				false // URLs?
				);

		// NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...

		// set the background color for the chart...
		chart.setBackgroundPaint(Color.white);

		// get a reference to the plot for further customisation...
		CategoryPlot plot = (CategoryPlot) chart.getPlot();
		plot.setBackgroundPaint(Color.lightGray);
		plot.setDomainGridlinePaint(Color.white);
		plot.setDomainGridlinesVisible(true);
		plot.setRangeGridlinePaint(Color.white);

		// ******************************************************************
		//  More than 150 demo applications are included with the JFreeChart
		//  Developer Guide...for more information, see:
		//
		//  >   http://www.object-refinery.com/jfreechart/guide.html
		//
		// ******************************************************************

		// set the range axis to display integers only...
		final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
		rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

		// disable bar outlines...
		BarRenderer renderer = (BarRenderer) plot.getRenderer();
		renderer.setDrawBarOutline(false);

		// set up gradient paints for series...
		GradientPaint gp0 = new GradientPaint(0.0f, 0.0f, Color.blue, 0.0f,
				0.0f, new Color(0, 0, 64));
		GradientPaint gp1 = new GradientPaint(0.0f, 0.0f, Color.green, 0.0f,
				0.0f, new Color(0, 64, 0));
		GradientPaint gp2 = new GradientPaint(0.0f, 0.0f, Color.red, 0.0f,
				0.0f, new Color(64, 0, 0));
		renderer.setSeriesPaint(0, gp0);
		renderer.setSeriesPaint(1, gp1);
		renderer.setSeriesPaint(2, gp2);

		CategoryAxis domainAxis = plot.getDomainAxis();
		domainAxis.setCategoryLabelPositions(CategoryLabelPositions
				.createUpRotationLabelPositions(Math.PI / 6.0));
		ChartPanel panel = new ChartPanel(chart, false);
		panel.setPreferredSize(new Dimension(500, 270));
		
		setViewPanel(panel,panel.toString(),"Extrema View");
		return null;
	}	
	public void zoomOut(){
		ChartPanel panel = (ChartPanel)getViewPanel(0);
		panel.zoomOutBoth(1.0, 1.0);
	}
	public void  zoomIn(){
		ChartPanel panel = (ChartPanel)getViewPanel(0);
		panel.zoomInBoth(1.0, 1.0);	
	}
	public void zoomDefault() {
		ChartPanel panel = (ChartPanel)getViewPanel(0);
		panel.restoreAutoBounds();
	}
	public void save(String fileName){
		ChartPanel panel = (ChartPanel)getViewPanel(0);
		ParameterBlock pb = new ParameterBlock();
		BufferedImage im = panel.getChart().createBufferedImage(panel.getWidth(), panel.getHeight(), BufferedImage.TYPE_INT_RGB, null);
		pb.addSource(im);
		pb.add(fileName);
		pb.add("JPEG");
		JAI.create("filestore", pb, null);		
	}
}
